<?php

if(!defined('BASEPATH')) exit('No direct Script access allowed');

	class Category extends CI_Controller {
		function __construct() {
			parent::__construct();
			
			if(!$this->ion_auth->logged_in()){
				redirect('auth/login');
			}
			$this->load->library('form_validation');
			$this->load->model('category/category_model');
			$this->load->library('datatables');
		}
		function get(){
			echo $this->category_model->get();
		}		
		function create_category(){
			$this->Data['page_title'] = "Create Category";
			$meta['page_title'] = "Create Category";
			$this->form_validation->set_rules('name','Category Name','required|is_unique[category.name]');
			$this->form_validation->set_rules('description','Category Description','required|is_unique[category.description]');
			if($this->form_validation->run() ==true){
				$name = $this->input->post('name');
				$description = $this->input->post('description');
				$additional_data=array( 
				'name'=>$this->input->post('name'),
				'description'=>$this->input->post('description')
				);		
			}
			if($this->form_validation->run() == true){
				$this->category_model->insert($name,$description,$additional_data);
				redirect("category/category/get_all_category");
			
			}
			else{			
			//display the create user form
			//set the flash data error message if there is one
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['name'] = array(
				'name'  => 'name',
				'id'    => 'name',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('name'),
			);
			$this->data['description'] = array(
				'name'  => 'description',
				'id'    => 'description',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('description'),
			);
		//	print_r($this->data);exit;

			$this->load->view('common/header',$meta);
			$this->load->view('category/create_category', $this->data);
			$this->load->view('common/footer');
			}		
		}
		function get_all_category(){
			$meta['page_title']  = "Category List";
			$this->data['message'] = "Category Added Successfully";
			$this->load->view('common/header',$meta);
			$this->load->view('category/view_category');
			$this->load->view('common/footer');		
		}
		function edit_category($id){
			$this->data['page_title'] = "Edit Category";
			if (!$this->ion_auth->logged_in()){
			redirect('auth', 'refresh');
			}
			$user = $this->category_model->edit_category($id)->row();
			$this->form_validation->set_rules('name', 'Category Name', 'required|xss_clean');
			$this->form_validation->set_rules('description', 'Category Description', 'required|xss_clean');
			if (isset($_POST) && !empty($_POST)){
				$data = array(
				'name' => $this->input->post('name'),
				'description'  => $this->input->post('description')
				);
				if ($this->form_validation->run() === TRUE){
				$this->category_model->update($user->id, $data);
				redirect("category/category/get_all_category");
				}
			}
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
			$this->data['user'] = $user;
			
			$this->data['name'] = array(
			'name'  => 'name',
			'id'    => 'name',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('name', $user->name),
			);
			$this->data['description'] = array(
			'name'  => 'description',
			'id'    => 'description',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('description', $user->description),
			);
			$this->data['submit'] = array(
			'name' => 'submit',
			'id'   => 'submit',
			'type' => 'submit',
			'value' =>'Update',
			'class' =>'btn btn-success'
			);
			$meta['page_title'] = "Edit Category";
			$this->load->view('common/header',$meta);
			$this->load->view('category/edit_category', $this->data, $id);
			$this->load->view('common/footer');
		}
		function delete_category($id){
		$this->category_model->delete($id);
		redirect("category/category/get_all_category");		
		}
	}
?>
